from djcelery.models import PeriodicTask
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from UiManager.models import ProjectInfo, PageInfo, ElementInfo, LocationInfo, TestCaseInfo, TestSuiteInfo, EnvInfo, \
    ResourceInfo, DeviceInfo, ReportInfo, LogInfo


def page_tool(request):
    username = request.session.get("username")
    url = request.path
    method = request.method
    if url == '/ui/project_list/':
        project_name = request.POST.get('search_project_name', '')
        project_name_session = request.session.get('project_name', None)
        if 'search_project_name' in request.POST.keys():
            obj = ProjectInfo.objects.filter(project_name__contains=project_name)
            request.session['project_name'] = project_name
        elif not project_name and project_name_session:
            project_name = project_name_session
            obj = ProjectInfo.objects.filter(project_name__contains=project_name)
        else:
            obj = ProjectInfo.objects.all()
            if 'project_name' in request.session.keys():
                del request.session['project_name']

    if url == '/ui/page_list/':
        select_project_name = int(request.POST.get('select_project_name', 0))
        search_page_name = request.POST.get('search_page_name', '')

        page_name_session = request.session.get('page_name', None)
        page_project_name_session = request.session.get('page_project_name', 0)

        project_list = ProjectInfo.objects.all()

        if not select_project_name and not page_project_name_session:
            obj = PageInfo.objects.all()
            request.session['page_project_name'] = 0
        elif select_project_name:
            obj = PageInfo.objects.filter(belong_project_id__exact=select_project_name)
            request.session['page_project_name'] = select_project_name
        elif method == 'GET' and page_project_name_session:
            select_project_name = page_project_name_session
            obj = PageInfo.objects.filter(belong_project_id__exact=select_project_name)
        elif method == 'POST' and page_project_name_session:
            obj = PageInfo.objects.all()
            request.session['page_project_name'] = 0

        if 'search_page_name' in request.POST.keys():
            obj = obj.filter(page_name__contains=search_page_name)
            request.session['page_name'] = search_page_name
        elif not search_page_name and page_name_session:
            search_page_name = page_name_session
            obj = obj.filter(page_name__contains=search_page_name)
        else:
            if 'page_name' in request.session.keys():
                del request.session['page_name']

    if url == '/ui/element_list/':
        select_project_name = int(request.POST.get('select_project_name', 0))
        select_page_name = int(request.POST.get('select_page_name', 0))
        search_element_name = request.POST.get('search_element_name', '')

        element_name_session = request.session.get('element_name', None)
        element_project_name_session = request.session.get('element_project_name', 0)
        element_page_name_session = request.session.get('element_page_name', 0)

        project_list = ProjectInfo.objects.all()
        location_list = LocationInfo.objects.all()

        # 项目筛选
        if not select_project_name and not element_project_name_session:
            obj = ElementInfo.objects.all()
            request.session['page_project_name'] = 0
        elif select_project_name:
            obj = ElementInfo.objects.filter(belong_page__belong_project_id__exact=select_project_name)
            request.session['element_project_name'] = select_project_name
        elif method == 'GET' and element_project_name_session:
            select_project_name = element_project_name_session
            obj = ElementInfo.objects.filter(belong_page__belong_project_id__exact=select_project_name)
        elif method == 'POST' and element_project_name_session:
            obj = ElementInfo.objects.all()
            request.session['element_project_name'] = 0

        # 页面筛选
        if not select_page_name and not element_page_name_session:
            request.session['element_page_name'] = 0
        elif select_page_name:
            obj = obj.filter(belong_page_id__exact=select_page_name)
            request.session['element_page_name'] = select_page_name
        elif method == 'GET' and element_page_name_session:
            select_page_name = element_page_name_session
            obj = obj.filter(belong_page_id__exact=select_page_name)
        elif method == 'POST' and element_page_name_session:
            request.session['element_page_name'] = 0

        if 'search_element_name' in request.POST.keys():
            obj = obj.filter(element_name__contains=search_element_name)
            request.session['element_name'] = search_element_name
        elif not search_element_name and element_name_session:
            search_element_name = element_name_session
            obj = obj.filter(element_name__contains=search_element_name)
        else:
            if 'element_name' in request.session.keys():
                del request.session['element_name']

    if url == '/ui/case_list/':
        select_project_name = int(request.POST.get('select_project_name', 0))
        search_case_name = request.POST.get('search_case_name', '')

        case_name_session = request.session.get('case_name', None)
        case_project_name_session = request.session.get('case_project_name', 0)

        project_list = ProjectInfo.objects.all()

        if not select_project_name and not case_project_name_session:
            obj = TestCaseInfo.objects.all()
            request.session['case_project_name'] = 0
        elif select_project_name:
            obj = TestCaseInfo.objects.filter(belong_project_id__exact=select_project_name)
            request.session['case_project_name'] = select_project_name
        elif method == 'GET' and case_project_name_session:
            select_project_name = case_project_name_session
            obj = TestCaseInfo.objects.filter(belong_project_id__exact=select_project_name)
        elif method == 'POST' and case_project_name_session:
            obj = TestCaseInfo.objects.all()
            request.session['case_project_name'] = 0

        if 'search_case_name' in request.POST.keys():
            obj = obj.filter(case_name__contains=search_case_name)
            request.session['case_name'] = search_case_name
        elif not search_case_name and case_name_session:
            search_case_name = case_name_session
            obj = obj.filter(case_name__contains=search_case_name)
        else:
            if 'case_name' in request.session.keys():
                del request.session['case_name']

    if url == '/ui/suite_list/':
        select_project_name = int(request.POST.get('select_project_name', 0))
        search_suite_name = request.POST.get('search_suite_name', '')

        suite_name_session = request.session.get('suite_name', None)
        suite_project_name_session = request.session.get('suite_project_name', 0)

        project_list = ProjectInfo.objects.all()
        env_list = EnvInfo.objects.all()

        if not select_project_name and not suite_project_name_session:
            obj = TestSuiteInfo.objects.all()
            request.session['suite_project_name'] = 0
        elif select_project_name:
            obj = TestSuiteInfo.objects.filter(belong_project_id__exact=select_project_name)
            request.session['suite_project_name'] = select_project_name
        elif method == 'GET' and suite_project_name_session:
            select_project_name = suite_project_name_session
            obj = TestSuiteInfo.objects.filter(belong_project_id__exact=select_project_name)
        elif method == 'POST' and suite_project_name_session:
            obj = TestSuiteInfo.objects.all()
            request.session['suite_project_name'] = 0

        if 'search_suite_name' in request.POST.keys():
            obj = obj.filter(suite_name__contains=search_suite_name)
            request.session['suite_name'] = search_suite_name
        elif not search_suite_name and suite_name_session:
            search_suite_name = suite_name_session
            obj = obj.filter(suite_name__contains=search_suite_name)
        else:
            if 'suite_name' in request.session.keys():
                del request.session['suite_name']

    if url == '/ui/task_list/':
        search_task_name = request.POST.get('search_task_name', '')
        task_name_session = request.session.get('task_name', None)

        project_list = ProjectInfo.objects.all()
        env_list = EnvInfo.objects.all()

        if 'search_task_name' in request.POST.keys():
            obj = PeriodicTask.objects.filter(name__contains=search_task_name)
            request.session['task_name'] = search_task_name
        elif not search_task_name and task_name_session:
            search_task_name = task_name_session
            obj = PeriodicTask.objects.filter(name__contains=search_task_name)
        else:
            obj = PeriodicTask.objects.all()
            if 'task_name' in request.session.keys():
                del request.session['task_name']
        obj = obj.filter(task__exact='UiManager.tasks.suite_delay_run')

    if url == '/ui/env_list/':
        search_env_name = request.POST.get('search_env_name', '')
        env_name_session = request.session.get('env_name', None)

        resource_list = ResourceInfo.objects.all()

        if 'search_env_name' in request.POST.keys():
            obj = EnvInfo.objects.filter(env_name__contains=search_env_name)
            request.session['env_name'] = search_env_name
        elif not search_env_name and env_name_session:
            search_env_name = env_name_session
            obj = EnvInfo.objects.filter(env_name__contains=search_env_name)
        else:
            obj = EnvInfo.objects.all()
            if 'env_name' in request.session.keys():
                del request.session['env_name']

    if url == '/ui/resource_list/':
        search_resource_name = request.POST.get('search_resource_name', '')
        resource_name_session = request.session.get('resource_name', None)

        if 'search_resource_name' in request.POST.keys():
            obj = ResourceInfo.objects.filter(resource_name__contains=search_resource_name)
            request.session['resource_name'] = search_resource_name
        elif not search_resource_name and resource_name_session:
            search_resource_name = resource_name_session
            obj = ResourceInfo.objects.filter(resource_name__contains=search_resource_name)
        else:
            obj = ResourceInfo.objects.all()
            if 'resource_name' in request.session.keys():
                del request.session['resource_name']

    if url == '/ui/device/':
        search_device_name = request.POST.get('search_device_name', '')
        device_name_session = request.session.get('device_name', None)

        if 'search_device_name' in request.POST.keys():
            obj = DeviceInfo.objects.filter(deviceid__contains=search_device_name)
            request.session['device_name'] = search_device_name
        elif not search_device_name and device_name_session:
            search_device_name = device_name_session
            obj = DeviceInfo.objects.filter(deviceid__contains=search_device_name)
        else:
            obj = DeviceInfo.objects.all()
            if 'device_name' in request.session.keys():
                del request.session['device_name']

    if url == '/ui/report_list/':
        search_report_name = request.POST.get('search_report_name', '')
        report_name_session = request.session.get('report_name', None)

        if 'search_report_name' in request.POST.keys():
            obj = ReportInfo.objects.filter(html_report_name__contains=search_report_name)
            request.session['report_name'] = search_report_name
        elif not search_report_name and report_name_session:
            search_report_name = report_name_session
            obj = ReportInfo.objects.filter(html_report_name__contains=search_report_name)
        else:
            obj = ReportInfo.objects.all()
            if 'report_name' in request.session.keys():
                del request.session['report_name']

    if url == '/ui/run_log_list/':
        search_run_log_name = request.POST.get('search_run_log_name', '')
        run_log_name_session = request.session.get('run_log_name', None)

        if 'search_run_log_name' in request.POST.keys():
            obj = LogInfo.objects.filter(log_name__contains=search_run_log_name)
            request.session['run_log_name'] = search_run_log_name
        elif not search_run_log_name and run_log_name_session:
            search_run_log_name = run_log_name_session
            obj = LogInfo.objects.filter(log_name__contains=search_run_log_name)
        else:
            obj = LogInfo.objects.all()
            if 'run_log_name' in request.session.keys():
                del request.session['run_log_name']

    # 生成paginator对象,定义每页显示12条记录
    if url == '/ui/task_list/':
        paginator = Paginator(obj, 12)
    else:
        paginator = Paginator(obj.order_by('-create_time'), 12)

    # 从前端获取当前的页码数,默认为1
    page = request.GET.get('page', 1)

    # 把当前的页码数转换成整数类型
    currentPage = int(page)
    try:
        obj = paginator.page(page)  # 获取当前页码的记录
    except PageNotAnInteger:
        obj = paginator.page(1)  # 如果用户输入的页码不是整数时,显示第1页的内容
    except EmptyPage:
        obj = paginator.page(paginator.num_pages)  # 如果用户输入的页数不在系统的页码列表中时,显示最后一页的内容
    return locals()
